package hong.jun.pet_home.service.product.impl;


import hong.jun.basic.base.service.impl.BaseServiceImpl;
import hong.jun.pet_home.domain.product.Product;
import hong.jun.pet_home.domain.product.ProductDetail;
import hong.jun.pet_home.mapper.ProductDetailMapper;
import hong.jun.pet_home.mapper.ProductMapper;
import hong.jun.pet_home.service.product.IProductService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.Date;

@Service
public class ProductServiceImpl extends BaseServiceImpl<Product> implements IProductService {
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private ProductDetailMapper detailMapper;

    /**
     * 新增
     */
    @Override
    public void save(Product product) {
        product.setCreatetime(new Date());
        super.save(product);
        ProductDetail detail = product.getDetail();
        detail.setProduct_id(product.getId());
        detailMapper.save(detail);
    }

    /**
     * 修改
     */
    @Override
    public void update(Product product) {
        super.update(product);
        ProductDetail detail = product.getDetail();
        detail.setProduct_id(product.getId());
        detailMapper.update(detail);
    }

    /**
     * 删除
     */
    @Override
    public void delete(Serializable id) {
        super.delete(id);
        detailMapper.remove(id);
    }

    /**
     * 批量删除
     */
    @Override
    public void deletes(Serializable[] ids) {
        super.deletes(ids);
        detailMapper.deletes(ids);
    }

    /**
     * 修改上下架状态
     *
     * @param ids
     */
    @Override
    public void on_offsale(Long[] ids, Integer state) {
        if (state == 0)
            productMapper.off_sale(ids);
        else
            productMapper.on_sale(ids);
    }
}
